package com.dingreading.cloud.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.*;
import com.dingreading.cloud.common.util.aliyun.OSSClientUtil;
import com.dingreading.cloud.common.util.wechat.WxPayBusiness;
import com.dingreading.cloud.web.entity.*;
import com.dingreading.cloud.web.service.*;
import com.dingreading.cloud.web.util.redis.RedisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.*;

@RefreshScope
@RestController
@RequestMapping("/mini/mdb/readingActivity")
@Api(tags = "叮叮门店宝小程序-好读活动管理")
public class MiniReadingActivityController {

    @Resource
    protected WebImageService webImageService;
    @Resource
    protected ReadingStudentService readingStudentService;
    @Resource
    protected ReadingStudentPunchService readingStudentPunchService;
    @Resource
    protected ReadingBookService readingBookService;
    @Resource
    protected ReadingStoreService readingStoreService;
    @Resource
    protected ReadingDateService readingDateService;
    @Resource
    protected WxMiniUserService miniUserService;
    @Resource
    protected RedisService redisService;

    @Value("${mini.store.appId}")
    private String miniAppId;
    @Value("${mini.store.appSecret}")
    private String miniSecret;

    public static final String appName = "叮叮门店宝";


    @ApiOperation(value = "获取小程序好读活动首页图片")
    @PostMapping(value = "/getHomeImg", produces = "application/json; charset=UTF-8")
    public R<Object> getHomeImg() {
        WebImage webImage = webImageService.getByType("xcx_mdb_read_home");

        return R.ok(webImage.getUrl());
    }


    @ApiOperation(value = "获取小程序登录用户的unionid和openid")
    @PostMapping(value = "/getUnionidAndOpenid", produces = "application/json; charset=UTF-8")
    public R<Object> getUnionidAndOpenid(
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid", required = false) String storeUid,
            @ApiParam(value = "分享小程序的用户标识") @RequestParam(value = "shareOpenid", required = false) String shareOpenid,
            @ApiParam(value = "渠道标识") @RequestParam(value = "channelUid", required = false) String channelUid,
            @ApiParam(value = "wx.login 接口获得临时登录凭证") @RequestParam(value = "code") String code
    ) throws Exception {
        if (StringUtils.isBlank(code))
            return R.fail("未传递小程序临时登录凭证");

        // 获取小程序登录用户信息
        JSONObject jsonObject = WxMiniUtil.Init().getUnionidAndOpenid(miniAppId, miniSecret, code);

        String openid = jsonObject.getString("openid");
        if (StringUtils.isNotBlank(openid)) {
            String unionid = jsonObject.getString("unionid");
            WxMiniUser miniUser = miniUserService.getByOpenid(openid);
            Date time = DateUtil.getNowDatetime();
            if (miniUser == null) {
                miniUser = new WxMiniUser();
                if (StringUtils.isNotBlank(unionid))
                    miniUser.setUnionid(unionid);
                miniUser.setOpenid(openid);
                miniUser.setShareOpenid(shareOpenid);
                miniUser.setOriginStoreUid(storeUid);
                miniUser.setRemark("门店宝小程序-好读活动");
                miniUser.setSource(EnumType.Source.XCX_MDB.getName());
                miniUser.setAddTime(time);
            }
            if (StringUtils.isNotBlank(storeUid))
                miniUser.setStoreUid(storeUid);
            if (StringUtils.isNotBlank(channelUid)) {
                miniUser.setChannelUid(channelUid);
                miniUser.setChannelName(getChannelName(channelUid));
            }
            miniUser.setEditTime(time);
            miniUserService.saveOrUpdate(miniUser);

            jsonObject.put("storeUid", miniUser.getStoreUid());
            jsonObject.put("channelUid", miniUser.getChannelUid());
        }

        return R.ok(jsonObject);
    }


    @ApiOperation(value = "获取报名的学生数量和分享成功数量")
    @PostMapping(value = "/getSignCntAndShareCnt", produces = "application/json; charset=UTF-8")
    public R<Object> getSignCntAndShareCnt(
            @ApiParam(value = "小程序用户唯一标识") @RequestParam(value = "openid") String openid
    ) {
        long signCnt = readingStudentService.getStudentSignCnt();

        ReadingDate readingDate = readingDateService.getById(1L);
        if (readingDate != null) {
            Double signNum = readingDate.getSignNum();
            if (signNum != null && signNum > 0) {
                double v = signCnt * signNum;

                // 向上取整
                double ceil = Math.ceil(v);
                signCnt = (int) ceil;

            }
        }

        long shareCnt = 0;
//        if (StringUtils.isNotBlank(openid))
//            shareCnt = readingStudentService.getShareCnt(openid);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("signCnt", signCnt);
        jsonObject.put("shareCnt", shareCnt);

        return R.ok(jsonObject);
    }


    @ApiOperation(value = "获取参加活动的门店集合")
    @PostMapping(value = "/getStoreList", produces = "application/json; charset=UTF-8")
    public R<Object> getStoreList(
            @ApiParam(value = "门店标识") @RequestParam(value = "storeUid", required = false) String storeUid
    ) {
        List<ReadingStore> list = readingStoreService.getByIdsOrderByUidDesc(storeUid);

        return R.ok(list);
    }


    @ApiOperation(value = "获取书籍")
    @PostMapping(value = "/getBookList", produces = "application/json; charset=UTF-8")
    public R<Object> getBookList(
            @ApiParam(value = "年级 一年级:1，二年级:2，三年级:3，四年级:4，五年级:5") @RequestParam(value = "grade") Integer grade,
            @ApiParam(value = "学生uid") @RequestParam(value = "studentUid", required = false) String studentUid
    ) {
        List<ReadingBook> list = readingBookService.listByGrade(grade);

        if (CollectionUtils.isNotEmpty(list)) {
            String studentBookName = "";
            if (StringUtils.isNotBlank(studentUid)) {
                ReadingStudent student = readingStudentService.getStudent(studentUid);
                if (student != null)
                    studentBookName = student.getBookName();
            }

            List<String> bookNames = new ArrayList<>();
            for (ReadingBook readingBook : list) {
                if (readingBook.getLimitNum() != null && !readingBook.getBookName().equals(studentBookName))
                    bookNames.add(readingBook.getBookName());
            }

            if (CollectionUtils.isNotEmpty(bookNames)) {
                Map<String, Long> bookCntMap = readingStudentService.haveSignStudentBookCnt(bookNames);

                ListIterator<ReadingBook> iterator = list.listIterator();
                while (iterator.hasNext()) {
                    ReadingBook readingBook = iterator.next();

                    Integer limitNum = readingBook.getLimitNum();
                    if (limitNum != null) {
                        Long bookCnt = bookCntMap.get(readingBook.getBookName());
                        if (NepUtil.nullToZero(bookCnt) >= NepUtil.nullToZero(limitNum))
                            iterator.remove();
                    }
                }
            }
        }

        return R.ok(list);
    }


    @ApiOperation(value = "查看学生信息")
    @PostMapping(value = "/getStudent", produces = "application/json; charset=UTF-8")
    public R<Object> getStudent(
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "学生uid") @RequestParam(value = "studentUid") String studentUid
    ) {
        ReadingStudent data = readingStudentService.getStudent(studentUid);
        if (data == null)
            return R.fail("学生查询不存在");

        return R.ok(data);
    }


    @ApiOperation(value = "好读活动报名")
    @PostMapping(value = "/studentSignUp", produces = "application/json; charset=UTF-8")
    public R<Object> studentSignUp(
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "学生uid") @RequestParam(value = "uid", required = false) String uid,
            @ApiParam(value = "小程序用户在开放平台的唯一标识符") @RequestParam(value = "unionid", required = false) String unionid,
            @ApiParam(value = "小程序用户唯一标识") @RequestParam(value = "openid") String openid,
            @ApiParam(value = "分享小程序的用户标识") @RequestParam(value = "shareOpenid", required = false) String shareOpenid,
            @ApiParam(value = "渠道标识") @RequestParam(value = "channelUid", required = false) String channelUid,
            @ApiParam(value = "姓名") @RequestParam(value = "fullName") String fullName,
            @ApiParam(value = "年级 一年级:1，二年级:2，三年级:3，四年级:4，五年级:5") @RequestParam(value = "grade") Integer grade,
            @ApiParam(value = "联系号码") @RequestParam(value = "mob") String mob,
            @ApiParam(value = "学校名称") @RequestParam(value = "schoolName") String schoolName,
            @ApiParam(value = "书籍id") @RequestParam(value = "bookId") Long bookId,
            @ApiParam(value = "书籍名称") @RequestParam(value = "bookName") String bookName
    ) throws Exception {
        if (StringUtils.isBlank(storeUid))
            return R.fail("门店标识不能为空");
        if (StringUtils.isBlank(openid))
            return R.fail("小程序的用户唯一标识不能为空");
        if (StringUtils.isBlank(fullName))
            return R.fail("请填写姓名");
        if (fullName.length() < 2 || fullName.length() > 6)
            return R.fail("请填写正确的姓名");
        if (!StringUtil.isChinese(fullName))
            return R.fail("请填写正确的姓名");
        if (StringUtils.isBlank(schoolName))
            return R.fail("请填写学校名称");
        if (StringUtils.isBlank(mob))
            return R.fail("请填写联系号码");
        if (mob.length() != 11)
            return R.fail("联系号码格式错误");
        if (null == grade)
            return R.fail("请选择年级");
        if (StringUtils.isBlank(bookName))
            return R.fail("请选择书籍");

        // 验证能否报名
        ReadingDate readingDate = readingDateService.getById(1L);
        String msg = judgeSignUp(readingDate);
        if (StringUtils.isNotBlank(msg))
            return R.fail(msg);

        ReadingStore readingStore = readingStoreService.getByUid(storeUid);
        if (null == readingStore)
            return R.fail("领书地址获取失败");

        ReadingStudent exStudent = readingStudentService.haveFullNameAndMob(readingStore.getCorpUid(), uid, fullName, mob);
        if (exStudent != null)
            return R.fail("姓名和联系号码已报名,请修改");

        // 学生报名，录入学生信息
        String source = EnumType.Source.XCX_MDB.getName();
        Date time = DateUtil.getNowDatetime();

        ReadingStudent student = null;
        if (StringUtils.isBlank(uid)) {
            if (StringUtils.isNotBlank(channelUid)) {
                if (channelUid.equals("13")) {
                    // 屯小本部
                    long l = readingStudentService.haveChannelUid(channelUid);
                    if (l >= 200)
                        return R.fail("本活动地点报名已满");
                } else if (channelUid.equals("11")) {
                    // 地推
                    Integer dtNum = readingDate.getDtNum();
                    if (dtNum != null) {
                        long l = readingStudentService.haveChannelUid(channelUid);
                        if (l >= dtNum)
                            return R.fail("本活动地点报名已满");
                    }
                }
            }

            List<ReadingStudent> studentList = readingStudentService.getStudentByOpenid(openid);
            if (CollectionUtils.isNotEmpty(studentList) && studentList.size() >= 3)
                return R.fail("最多报名3个学生");

            student = new ReadingStudent();
            uid = KitUtil.getUuid();
            student.setCorpUid(readingStore.getCorpUid());
            student.setUid(uid);
            student.setUnionid(unionid);
            student.setOpenid(openid);
            if (StringUtils.isNotBlank(channelUid)) {
                student.setChannelUid(channelUid);
                student.setChannelName(getChannelName(channelUid));
            } else {
                WxMiniUser miniUser = miniUserService.getByOpenid(openid);
                if (miniUser != null && StringUtils.isNotBlank(miniUser.getChannelUid())) {
                    student.setChannelUid(miniUser.getChannelUid());
                    student.setChannelName(getChannelName(miniUser.getChannelUid()));
                }
            }
            // 排除自己分享给自己
            if (!openid.equals(shareOpenid))
                student.setShareOpenid(shareOpenid);
            student.setAddStaffUid(null);
            student.setAddStaffName(source);
            student.setAddTime(time);
        } else {
            student = readingStudentService.getStudent(uid);
            if (student == null)
                return R.fail("学生信息不存在");
        }
        student.setStoreUid(readingStore.getStoreUid());
        student.setStoreName(readingStore.getStoreName());
        student.setFullName(fullName);
        student.setSchoolName(schoolName);
        student.setMob(mob);
        student.setGrade(grade);
        student.setBookId(bookId);
        student.setBookName(bookName);
        student.setEditTime(time);
        readingStudentService.saveOrUpdate(student);

        // 埋点
//        ApplyBrowseLog log = new ApplyBrowseLog();
//        log.setApplyName(appName);
//        log.setPageName("好读活动报名");
//        // 门店uid+渠道uid
//        String userUid = readingStore.getStoreUid() + "&" + uid;
//        log.setUserUid(userUid);
//        log.setAddTime(time);
//        browseLogService.saveOrUpdate(log);

        return R.ok();
    }


    @ApiOperation(value = "获取小程序用户已报名的学生集合")
    @PostMapping(value = "/getStudentList", produces = "application/json; charset=UTF-8")
    public R<Object> getStudentList(
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid", required = false) String storeUid,
            @ApiParam(value = "小程序用户唯一标识") @RequestParam(value = "openid") String openid
    ) {
        List<ReadingStudent> list = readingStudentService.getStudentByOpenid(openid);

        return R.ok(list);
    }


    @ApiOperation(value = "获取学生的打卡记录")
    @PostMapping(value = "/getStudentPunchList", produces = "application/json; charset=UTF-8")
    public R<Object> getStudentPunchList(
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "学生uid") @RequestParam(value = "studentUid") String studentUid
    ) {
        List<ReadingStudentPunch> list = readingStudentPunchService.getStudentPunchList(studentUid);

        return R.ok(list);
    }


    @ApiOperation(value = "学生打卡")
    @PostMapping(value = "/studentPunch", produces = "application/json; charset=UTF-8")
    public R<Object> studentPunch(
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "学生uid") @RequestParam(value = "studentUid") String studentUid,
            @ApiParam(value = "题目序号") @RequestParam(value = "itemNum") Integer itemNum,
            @ApiParam(value = "文件类型 1.图片 2.视频") @RequestParam(value = "fileType") Integer fileType,
            @ApiParam(value = "打卡文件地址") @RequestParam(value = "url") String url
    ) throws Exception {
        // 验证能否打卡
        ReadingDate readingDate = readingDateService.getById(1L);
        String msg = judgePunch(readingDate);
        if (StringUtils.isNotBlank(msg))
            return R.fail(msg);

        if (itemNum == null)
            return R.fail("请选择闯关");
        if (StringUtils.isBlank(url))
            return R.fail("请上传文件");

        ReadingStudent student = readingStudentService.getStudent(studentUid);
        if (student == null)
            return R.fail("会员信息不存在");

        ReadingStudentPunch punch = readingStudentPunchService.getStudentPunch(studentUid, itemNum);
        Date time = DateUtil.getNowDatetime();
        String punchUrl = "";
        if (punch == null) {
            // 题目第一次打卡
            punch = new ReadingStudentPunch();
            punch.setCorpUid(student.getCorpUid());
            punch.setStoreUid(student.getStoreUid());
            punch.setStoreName(student.getStoreName());
            punch.setStudentUid(student.getUid());
            punch.setFullName(student.getFullName());
            punch.setMob(student.getMob());
            punch.setItemNum(itemNum);
            punch.setFileType(fileType);
            punch.setMdyNum(0);
            punch.setAddTime(time);

            if (StringUtils.isNotBlank(student.getOpenid())) {
                WxMiniUser miniUser = miniUserService.getByOpenid(student.getOpenid());
                if (miniUser != null && StringUtils.isNotBlank(miniUser.getChannelUid())) {
                    punch.setChannelUid(miniUser.getChannelUid());
                    punch.setChannelName(getChannelName(miniUser.getChannelUid()));
                }
            }
        } else {
            // 判断修改次数
            Integer mdyNum = punch.getMdyNum();
            if (readingDate != null) {
                Integer punchNum = readingDate.getPunchNum();
                if (punchNum != null && punchNum > 0 && mdyNum >= punchNum) {
                    return R.fail("修改次数已达上限");
                }
            }
            // 修改次数+1
            punch.setMdyNum(mdyNum + 1);
            // 原打卡文件
            punchUrl = punch.getUrl();
        }
        punch.setUrl(url);
        punch.setEditTime(time);
        readingStudentPunchService.saveOrUpdate(punch);

        // 删除阿里云上的原打卡图片
        if (StringUtils.isNotBlank(punchUrl))
            deleteOssFiles(punchUrl);

        return R.ok();
    }


    @ApiOperation(value = "更新学生头像")
    @PostMapping(value = "/updateStudentImg")
    public R<Object> updateStudentImg(
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "学生uid") @RequestParam(value = "studentUid") String studentUid,
            @ApiParam(value = "图片路径") @RequestParam(value = "img") String img
    ) {
        // 验证能否报名
        ReadingDate readingDate = readingDateService.getById(1L);
        String msg = judgeSignUp(readingDate);
        if (StringUtils.isNotBlank(msg))
            return R.fail(msg);

        if (StringUtils.isBlank(img))
            return R.fail("未上传头像");

        ReadingStudent student = readingStudentService.getStudent(studentUid);
        if (student == null)
            return R.fail("学生信息获取失败");

        String studentImg = student.getImg();
        boolean l = readingStudentService.updateStudentImg(studentUid, img);
        if (!l)
            return R.fail("更新头像失败");

        // 删除阿里云上的原头像
        if (StringUtils.isNotBlank(studentImg)) {
            deleteOssFiles(studentImg);
        }

        return R.ok();
    }


    @ApiOperation(value = "上传文件至阿里云oss")
    @PostMapping(value = "/uploadFile")
    public R<Object> uploadFile(
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "学生uid") @RequestParam(value = "studentUid") String studentUid,
            @ApiParam(value = "图片文件") @RequestPart(value = "image") MultipartFile file,
            @ApiParam(value = "文件类型 1.图片 2.视频") @RequestParam(value = "fileType") Integer fileType,
            @ApiParam(value = "上传类型 1.上传头像 2.上传打卡") @RequestParam(value = "type") Integer type
    ) throws Exception {
        if (type.equals(1)) {
            // 验证能否报名
            ReadingDate readingDate = readingDateService.getById(1L);
            String msg = judgeSignUp(readingDate);
            if (StringUtils.isNotBlank(msg))
                return R.fail(msg);
        } else {
            // 验证能否打卡
            ReadingDate readingDate = readingDateService.getById(1L);
            String msg = judgePunch(readingDate);
            if (StringUtils.isNotBlank(msg))
                return R.fail(msg);
        }

        if (fileType.equals(1)) {
            boolean m = FileUtil.checkFileSize(file.getSize(), 5, "M");
            if (!m)
                return R.fail("上传图片大小不能超过5M");
        } else {
            return R.fail("只能上传图片");
        }

        OSSClientUtil ossClientUtil = new OSSClientUtil();
        ossClientUtil.initOss();
        // 获取阿里云上传的文件路径
        String todayDir = ossClientUtil.getTodaySplitDir(OSSClientUtil.xcxMdbReadingDir);

        String resultUrl = ossClientUtil.uploadImg2Oss(todayDir, file);
        ossClientUtil.destory();
        if (StringUtils.isBlank(resultUrl))
            return R.fail("上传失败");

        return R.ok(todayDir + resultUrl);
    }


    @ApiOperation(value = "验证视频时长")
    @PostMapping(value = "/judgeVideoDuration")
    public R<Object> judgeVideoDuration(
            @ApiParam(value = "视频时长") @RequestParam(value = "duration") Double duration
    ) throws Exception {
        // 验证能否打卡
        ReadingDate readingDate = readingDateService.getById(1L);
        String msg = judgePunch(readingDate);
        if (StringUtils.isNotBlank(msg))
            return R.fail(msg);

        if (duration == null || duration <= 0)
            return R.fail("时长必须大于0");

        Integer videoDuration = readingDate.getVideoDuration();
        if (videoDuration != null && duration > videoDuration)
            return R.fail("视频时长不能超过" + videoDuration + "秒");

        return R.ok();
    }


    @ApiOperation(value = "验证能否闯关")
    @PostMapping(value = "/judgePunch")
    public R<Object> judgePunch(
            @ApiParam(value = "学生uid") @RequestParam(value = "studentUid") String studentUid,
            @ApiParam(value = "题目序号") @RequestParam(value = "itemNum") Integer itemNum
    ) throws Exception {
        // 验证能否打卡
        ReadingDate readingDate = readingDateService.getById(1L);
        String msg = judgePunch(readingDate);
        if (StringUtils.isNotBlank(msg))
            return R.fail(msg);

        ReadingStudentPunch punch = readingStudentPunchService.getStudentPunch(studentUid, itemNum);
        if (punch != null) {
            // 判断修改次数
            Integer mdyNum = punch.getMdyNum();
            if (readingDate != null) {
                Integer punchNum = readingDate.getPunchNum();
                if (punchNum != null && punchNum > 0 && mdyNum >= punchNum) {
                    return R.fail("修改次数已达上限");
                }
            }
        }
        return R.ok();
    }


    @ApiOperation(value = "获取门店的小程序码图片")
    @PostMapping(value = "/getMiniCode", produces = "application/json; charset=UTF-8")
    public R<Object> getMiniCode(
            @ApiParam(value = "门店id") @RequestParam(value = "storeId") Long storeId,
            @ApiParam(value = "渠道uid") @RequestParam(value = "channelUid") String channelUid
    ) throws Exception {
        ReadingStore readingStore = readingStoreService.getByUid(storeId + "");
        if (null == readingStore)
            return R.fail("门店获取失败");

        // 生产小程序标识
        String scene = storeId + "@" + channelUid;
        // 小程序路径
        String path = "pages/apply/apply";

        // 获取小程序accessToken
        String redisKey = RedisParam.WEIXIN_ACCESS_TOKEN + miniAppId;
        String accessToken = (String) redisService.get(redisKey);
        if (StringUtils.isBlank(accessToken)) {
            accessToken = WxPayBusiness.Init().getAccessToken(miniAppId, miniSecret);
            if (StringUtils.isBlank(accessToken))
                return R.fail("微信小程序access_token获取失败");
            redisService.set(redisKey, accessToken, 7200);
        }
        // 生成小程序码
        JSONObject jsonObject = WxMiniUtil.Init().getCodeUnLimit(accessToken, path, scene, 280);
        if (null == jsonObject)
            return R.fail("小程序码获取失败");
        Integer status = jsonObject.getInteger("status");

        if (status.equals(0)) {
            byte[] bytes = jsonObject.getBytes("data");
            // 图片下载到本地
            String imgUrl = WxMiniUtil.Init().buff2Image(bytes);
            return R.ok(imgUrl);
        } else {
            String data = jsonObject.getString("data");
            JSONObject jsonObj = JSONObject.parseObject(data);
            Integer errcode = jsonObj.getInteger("errcode");
            String errmsg = jsonObj.getString("errmsg");
            return R.fail("小程序码获取失败" + errcode + "," + errmsg);
        }
    }

    /**
     * 验证能否报名
     */
    private String judgeSignUp(ReadingDate readingDate) {
        if (readingDate == null)
            return "活动日期获取失败";
        String beginDate = readingDate.getSignBeginDate();
        String endDate = readingDate.getSignEndDate();
        if (StringUtils.isBlank(beginDate) || StringUtils.isBlank(endDate))
            return "报名日期获取失败";
        // 判读活动开始时间和结束时间
        if (judgeSignUpDate(beginDate, endDate))
            return "报名日期:" + beginDate + "至" + endDate;
        return "";
    }

    /**
     * 验证能否打卡
     */
    private String judgePunch(ReadingDate readingDate) {
        if (readingDate == null)
            return "活动日期获取失败";
        String beginDate = readingDate.getPunchBeginDate();
        String endDate = readingDate.getPunchEndDate();
        if (StringUtils.isBlank(beginDate) || StringUtils.isBlank(endDate))
            return "打卡日期获取失败";
        // 判读活动开始时间和结束时间
        if (judgeSignUpDate(beginDate, endDate))
            return "打卡日期:" + beginDate + "至" + endDate;
        return "";
    }

    /**
     * 验证开始和结束日期
     */
    private boolean judgeSignUpDate(String beginDate, String endDate) {
        try {
            // 活动开始时间和结束时间
            Date bgnDate = DateUtil.parseDate(beginDate);
            Date enDate = DateUtil.parseDate(endDate);
            String today = DateUtil.getNowDateStr();
            Date todayDate = DateUtil.parseDate(today);
            if (todayDate.getTime() < bgnDate.getTime() || todayDate.getTime() > enDate.getTime()) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 删除阿里云上存储的文件
     */
    private void deleteOssFiles(String fileUrl) {
        // 生产环境且阿里云文件地址不为空，才能删除
        // 删除阿里云图片
        OSSClientUtil ossClientUtil = new OSSClientUtil();
        ossClientUtil.initOss();
        ossClientUtil.deleteFile(OSSClientUtil.publicBucket, fileUrl);
    }

    /**
     * 获取渠道名称
     */
    private String getChannelName(String channelUid) {
        if (channelUid.equals("1")) {
            return "1店内部";
        } else if (channelUid.equals("2")) {
            return "1店外部";
        } else if (channelUid.equals("3")) {
            return "2店内部";
        } else if (channelUid.equals("4")) {
            return "2店外部";
        } else if (channelUid.equals("5")) {
            return "3店内部";
        } else if (channelUid.equals("6")) {
            return "3店外部";
        } else if (channelUid.equals("7")) {
            return "4店内部";
        } else if (channelUid.equals("8")) {
            return "4店外部";
        } else if (channelUid.equals("9")) {
            return "5店内部";
        } else if (channelUid.equals("10")) {
            return "5店外部";
        } else if (channelUid.equals("11")) {
            return "地推";
        } else if (channelUid.equals("12")) {
            return "户外活动";
        } else if (channelUid.equals("13")) {
            return "屯小本部";
        } else {
            return "";
        }
    }

}
